<template>
    <div class="mine">
        <div class="top">
            <div class="left">
                <img :src="headImg" alt="">
                <p @click="loginFn">{{ islogin ? username : "点击登录" }} </p>
            </div>
            <p class="right" @click="loginout"><van-icon :name="islogin ? 'cross' : 'arrow'" /></p>
        </div>
        <div class="bottom"></div>
        <!-- 遮罩层 -->
        <van-popup v-model:show="isshow" :style="{ padding: '10px' }">
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field v-model="username" name="用户名" label="用户名" placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]" />
                    <van-field v-model="pwd" type="password" name="密码" label="密码" placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]" />
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                        提交
                    </van-button>
                </div>
            </van-form>
        </van-popup>
    </div>
</template>

<script setup lang='ts'>
import { ref } from 'vue';
// 引入头像
import headImg from '@/assets/image/head.png'
// 引入登录接口
import { loginapi } from '@/api/mine'

// 导入创建user仓库的方法
import { useUserStore } from '@/stores/user'
import { storeToRefs } from 'pinia'
// 导入消息提示框方法
import { showConfirmDialog } from 'vant';

// 导入 useRouter方法
import { useRouter } from 'vue-router';
const router = useRouter() // 获取了router实例对象 === this.$router
//创建仓库
const userStore = useUserStore()
console.log('userStore', userStore);
// 解构响应式数据
const { token, userinfo } = storeToRefs(userStore)
// 解构actions方法
const { settoken, setuserinfo } = userStore

const isshow = ref<boolean>(false)
//定义表单内容
const username = ref('');
const pwd = ref('');

// 点击登录
const loginFn = () => {
    // 01: 判断未登录显示登录表单
    if (!islogin.value) {
        // 显示登录表单
        isshow.value = true;
    }
}

// 登录提交
const onSubmit = (values: {
    '密码': string,
    '用户名': string
}) => {
    console.log('submit', values);
    // 调用登录接口
    loginapi({
        username: values['用户名'],
        pwd: values['密码']
    }).then(res => {
        console.log('res', res);
        // 01: 将用户token 和userinfo 存到本地
        if (res.data.errno == 0) {
            settoken(res.data.data.token)
            setuserinfo(res.data.data.userInfo)
            // 02: 隐藏模态框
            isshow.value = false
            // 03: 修改登录状态为true
            islogin.value = true
        }
    })
};

// 定义登录状态 (默认false)
const islogin = ref(false);
if (localStorage.getItem('pinia-user')) {
    // 表示本地有内容
    islogin.value = true
    // console.log(JSON.parse(localStorage.getItem('pinia-user') as string));
    username.value = JSON.parse(localStorage.getItem('pinia-user') as string).userinfo.username
}


// 退出登录
const loginout = () => {
    if (islogin.value) {
        //当为登录时,执行退出登录的操作
        showConfirmDialog({
            title: '提示',
            message:
                '确认要退出吗?',
        })
            .then(() => {
                console.log('confirm');
                // 01: 清除本地登录信息
                settoken('');
                setuserinfo({});
                setTimeout(() => {
                    localStorage.removeItem('pinia-user')
                }, 0)
                //刷新当前页面组件
                router.go(0)
            })
            .catch(() => {
                // on cancel
                console.log('cancel')
            });
    }
}


</script>
<style scoped lang="scss">
.mine {
    .top {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px;
        box-sizing: border-box;
        background-color: #333;
        color: white;

        .left {
            display: flex;
            align-items: center;

            p {
                margin-left: 5px;
            }
        }

        img {
            width: 70px;
            height: 70px;
            border-radius: 50%;
        }


    }
}
</style>